package com.pucong.gitee.database.backup.business.service.service;

import com.pucong.gitee.database.backup.business.service.cover.ComputerServiceBeanConvert;
import com.pucong.gitee.database.backup.business.service.mapper.ServiceComputerMapper;
import com.pucong.gitee.database.backup.business.service.util.cmdOs.WebSSHService;
import com.pucong.gitee.database.backup.common.entity.service.ServiceComputer;
import com.pucong.gitee.database.backup.common.entity.service.dto.ServiceComputerContnectTestDto;
import com.pucong.gitee.database.backup.common.entity.service.dto.ServiceComputerSaveDto;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.utils.UUID;
import com.pucong.gitee.database.backup.utils.UserRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ServiceComputerServiceImpl implements ServiceComputerService {

    @Autowired
    private ServiceComputerMapper serviceComputerMapper;

    @Autowired
    private ComputerServiceBeanConvert computerServiceBeanConvert;

    @Autowired
    private WebSSHService webSSHService;

    @Override
    public int saveBaseInfo(ServiceComputerSaveDto dto) {
        ServiceComputer serviceComputer = computerServiceBeanConvert.dto2do(dto);
        if (StringUtils.isBlank(dto.getUuid())) {
            UserJwt loginUser = UserRequest.getLoginUser();
            serviceComputer.setUuid(UUID.uuid());
            serviceComputer.setCreateTime(new Date());
            serviceComputer.setCreateBy(loginUser.getUuid());
            serviceComputerMapper.insert(serviceComputer);
        } else {
            serviceComputer.setCreateTime(null);
            serviceComputer.setCreateBy(null);
            serviceComputerMapper.updateById(serviceComputer);
        }
        return 1;
    }

    @Override
    public ServiceComputer getServiceComputeById(String id) {
        ServiceComputer serviceComputer = serviceComputerMapper.selectById(id);
        serviceComputer.setPassword("");
        return serviceComputer;
    }

    @Override
    public boolean connectTest(ServiceComputerContnectTestDto dto) {
        return webSSHService.connectTest(dto);
    }
}
